package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.enums.BizEnum;
import com.qk.common.exception.BizException;
import com.qk.dto.role.AddRoleDTO;
import com.qk.dto.role.QueryPageRoleDTO;
import com.qk.dto.role.UpdateRoleDTO;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.mapper.UserMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @Author:gzc
 * @Date: 2025/10/06 19:40
 * @Description:
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<Role> queryListByPage(QueryPageRoleDTO queryPageRoleDTO) {
//        PageInfo<Role> pageInfo = PageHelper.startPage(roleQueryListByPageDto.getPage(), roleQueryListByPageDto.getPageSize())
//                .doSelectPageInfo(() -> roleMapper.queryPage(roleQueryListByPageDto));
//        return PageResult.<Role>builder().total(pageInfo.getTotal()).rows(pageInfo.getList()).build();
//        Page<Role> page = new Page<>(roleDTO.getPage(), roleDTO.getPageSize());
//        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.apply(StringUtils.hasText(roleDTO.getName()),
//                        "replace(role.name,' ','') like concat('%',replace({0},' ',''),'%')", roleDTO.getName())
//                .apply(StringUtils.hasText(roleDTO.getLabel()),
//                        "replace(role.label,' ','') like concat('%',replace({0},' ',''),'%')", roleDTO.getLabel())
//                .orderByDesc(Role::getUpdateTime);
//        IPage<Role> iPage = roleMapper.selectPage(page, queryWrapper);
        Page<Role> page = this.page(new PageDTO<>(queryPageRoleDTO.getPage(), queryPageRoleDTO.getPageSize()), Wrappers.lambdaQuery(Role.class)
                .apply(StringUtils.hasText(queryPageRoleDTO.getName()),
                        "replace(role.name,' ','') like concat('%',replace({0},' ',''),'%')", queryPageRoleDTO.getName())
                .apply(StringUtils.hasText(queryPageRoleDTO.getLabel()),
                        "replace(role.label,' ','') like concat('%',replace({0},' ',''),'%')", queryPageRoleDTO.getLabel())
                .orderByDesc(Role::getUpdateTime));
        return PageResult.<Role>builder().total(page.getTotal()).rows(page.getRecords()).build();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteRole(Integer id) {
        if (Objects.isNull(id)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        this.removeById(id);
    }

    @Override
    public void addRole(AddRoleDTO addRoleDTO) {
        if (BeanUtil.hasNullField(addRoleDTO, "remark")) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        Role role = BeanUtil.copyProperties(addRoleDTO, Role.class);
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        this.save(role);
    }

    @Override
    public Role queryById(Integer id) {
        if (Objects.isNull(id)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        return this.getById(id);
    }

    @Override
    public void updateRole(UpdateRoleDTO updateRoleDTO) {
        boolean hasNull = BeanUtil.hasNullField(updateRoleDTO,"createTime","updateTime");
        if (hasNull) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        Role role = BeanUtil.copyProperties(updateRoleDTO, Role.class);
        role.setUpdateTime(LocalDateTime.now());
        this.updateById(role);
    }

    @Override
    public List<Role> queryAll() {
        return this.baseMapper.selectList(null);
    }
}
